﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using System.Data;
using CUSTOMS.Modules.QLHS.Components;
using ASPNETVN.PORTAL.Components.Helpers;
using System.Collections;
using ASPNETVN.PORTAL.Components.Configuration;
using CUSTOMS.Components.SLXNK;
using CUSTOMS.Components;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Security;

namespace CUSTOMS.Modules.QLHS.Controls
{
    public partial class TimToKhai : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack == false)
            {
                // Tiêu đề.
                if (Request.QueryString["function"] != null)
                {
                    if (Request.QueryString["function"] == "ct")
                    {
                        lblTieuDe.Text = "Chọn tờ khai để nhập thông tin quản lý chứng từ";
                    }
                }

                txtMaLoaiHinh.Attributes.Add("onchange", string.Format("handler({0}, {1})", txtMaLoaiHinh.ClientID, ddlLoaiHinhMauDich.ClientID));
                ddlLoaiHinhMauDich.Attributes.Add("onchange", string.Format("handler({1}, {0})", txtMaLoaiHinh.ClientID, ddlLoaiHinhMauDich.ClientID));

                txtMaHaiQuan.Attributes.Add("onchange", string.Format("handler({0}, {1})", txtMaHaiQuan.ClientID, ddlDonViHaiQuan.ClientID));
                ddlDonViHaiQuan.Attributes.Add("onchange", string.Format("handler({1}, {0})", txtMaHaiQuan.ClientID, ddlDonViHaiQuan.ClientID));

                Hashtable settings = this.ParentControl.Settings;

                string mahaiquan = settings["MaHaiQuan"].ToString();
                txtMaHaiQuan.Text = mahaiquan;

                IDataReader drHaiQuan = DanhMuc.GetDonViHaiQuan(true);
                ddlDonViHaiQuan.DataSource = drHaiQuan;
                ddlDonViHaiQuan.DataBind();
                ddlDonViHaiQuan.SelectedValue = mahaiquan;

                IDataReader drLoaiHinh = DanhMuc.GetLoaiHinhMauDich();
                ddlLoaiHinhMauDich.DataSource = drLoaiHinh;
                ddlLoaiHinhMauDich.DataBind();
                drLoaiHinh.Close();

                txtNamDangKy.Text = DateTime.Today.Year.ToString();
            }
        }

        private void TimKiem()
        {
            // Doanh nghiep dang nhap. 
            string username = PortalSecurity.CurrentUser.Identity.Email.Split('@')[0];
            long madoanhnghiep = 0;
            bool isDoanhNghiep = Int64.TryParse(username, out madoanhnghiep);
            string madn = string.Empty;
            if (isDoanhNghiep)
            {
                madn = username;
            }
            else 
            {
                madn = string.Empty;
            }
            int sotokhai = string.IsNullOrEmpty(txtSoToKhai.Text) ? 0 : Convert.ToInt32(txtSoToKhai.Text);
            string maloaihinh = DanhMuc.GetMaLoaiHinhMauDich(ddlLoaiHinhMauDich.SelectedValue);
            string mahaiquan = ddlDonViHaiQuan.SelectedValue;
            int namdangky = string.IsNullOrEmpty(txtNamDangKy.Text) ? 0 : Convert.ToInt32(txtNamDangKy.Text);
            DateTime tungay = string.IsNullOrEmpty(txtTuNgay.Text) ? new DateTime(1900, 1, 1) : DateTime.Parse(txtTuNgay.Text);
            DateTime denngay = string.IsNullOrEmpty(txtDenNgay.Text) ? new DateTime(1900, 1, 1) : DateTime.Parse(txtDenNgay.Text);
            DataSet ds = ToKhaiMauDich.TimKiem(madn, sotokhai, maloaihinh, mahaiquan, namdangky, tungay, denngay);
            gvToKhai.DataSource = ds;
            gvToKhai.DataBind();
        }

        protected void btnTimKiem_Click(object sender, EventArgs e)
        {
            gvToKhai.PageIndex = 0;
            this.TimKiem();
        }

        protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            gvToKhai.PageIndex = ddl.SelectedIndex;
            this.TimKiem();
        }

        protected void gvToKhai_DataBound(object sender, EventArgs e)
        {
            if (gvToKhai.Rows.Count > 0)
            {
                // Retrieve the pager row.
                GridViewRow bottomPagerRow = gvToKhai.BottomPagerRow;
                GridViewRow topPagerRow = gvToKhai.TopPagerRow;

                // Retrieve the DropDownList and Label controls from the row.
                DropDownList topPageList = (DropDownList)topPagerRow.Cells[0].FindControl("PageDropDownList");
                DropDownList bottomPageList = (DropDownList)bottomPagerRow.Cells[0].FindControl("PageDropDownList");
                Label topPageLabel = (Label)topPagerRow.Cells[0].FindControl("PageLabel");
                Label bottomPageLabel = (Label)bottomPagerRow.Cells[0].FindControl("PageLabel");

                if (topPageList != null && bottomPageList != null)
                {
                    for (int i = 0; i < gvToKhai.PageCount; i++)
                    {
                        int pageNumber = i + 1;
                        ListItem item = new ListItem(pageNumber.ToString());
                        if (i == gvToKhai.PageIndex)
                        {
                            item.Selected = true;
                        }

                        topPageList.Items.Add(item);
                        bottomPageList.Items.Add(item);
                    }
                }

                if (topPageLabel != null && bottomPageLabel != null)
                {
                    topPageLabel.Text = bottomPageLabel.Text = " / " + gvToKhai.PageCount.ToString();
                }
            }
        }

        protected void gvToKhai_SelectedIndexChanged(object sender, EventArgs e)
        {
            IOrderedDictionary keys = gvToKhai.SelectedDataKey.Values;
            int sotokhai = Convert.ToInt32(keys[0]);
            string mahaiquan = keys[1].ToString().Trim();
            string maloaihinh = keys[2].ToString().Trim();
            int namdangky = Convert.ToInt32(keys[3]);

            if (Request.QueryString["function"] != null)
            {
                if (Request.QueryString["function"] == "ct")
                {
                    string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditChungTu", "sotokhai={0}&mahaiquan={1}&maloaihinh={2}&namdangky={3}", sotokhai, mahaiquan, maloaihinh, namdangky);
                    Response.Redirect(url);
                }
                else
                {
                    string url = HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, 0, "AddEditBienBan", "sotokhai={0}&mahaiquan={1}&maloaihinh={2}&namdangky={3}", sotokhai, mahaiquan, maloaihinh, namdangky);
                    Response.Redirect(url);
                }
            }
        }
    }
}